<template>
  <div class="">
    <!-- 日期选择器 -->
    <van-datetime-picker
      v-model="currentDate"
      type="date"
      title="选择年月日"
      :min-date="minDate"
      :max-date="maxDate"
      @confirm="onConfirm"
    />
    <!-- /日期选择器 -->
  </div>
</template>

<script>
// 导入 更新名字方法
import { updateUserBirthday } from '@/api/user.js'
import dayjs from 'dayjs'

export default {
  name: '',
  props: {
    // 原生日
    birthDay: {
      required: true
    }
  },
  data() {
    return {
      minDate: new Date(1990, 1, 1),
      maxDate: new Date(2101, 0, 1),
      currentDate: new Date(this.birthDay)
    }
  },
  created() {},
  methods: {
    // 执行修改 ----------------------------
    async onConfirm() {
      try {
        this.$toast.loading({
          message: '请稍后...', // 提示消息
          forbidClick: true, // 禁止点击关闭
          duration: 0 // 不关闭
        })
        const newDate = dayjs(this.currentDate).format('YYYY-MM-DD')
        await updateUserBirthday(newDate)

        this.$toast.success('更新成功~')
        this.$emit('onSuc', newDate)
      } catch (err) {
        this.$toast.fail('更新失败~')
      }
    }
  },
  computed: {},
  watch: {},
  components: {}
}
</script>

<style lang="less" scoped>
.van-nav-bar {
  background-color: #fff;
  /deep/ .van-nav-bar__title {
    color: #000 !important;
  }
}
</style>
